package com.wuhen.sql;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

//实现适配器
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
    private List<User> musers;
    private Context mcontext;

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView userId;
        TextView userName;
        TextView userPwd;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            userId = itemView.findViewById(R.id.id);
            userName = itemView.findViewById(R.id.name);
            userPwd = itemView.findViewById(R.id.pwd);
        }
    }

    public UserAdapter(Context context, List<User> users) {
        this.mcontext = context;
        this.musers = users;

    }

    //onCreateViewHolder方法创建一个viewHolder，viewholder可以理解为一条数据的展示布局，这里我们自定义类LinearViewHolder创建一个只有TextView的item
    //这里我们需要创建每条布局使用的layout：layout_linear_item
    @NonNull
    @Override
    public UserAdapter.ViewHolder onCreateViewHolder(@NonNull final ViewGroup viewGroup, int i) {

        final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);

        return new ViewHolder(view);
    }

    //onBindViewHolder方法为item的UI绑定展示数据，
    @Override
    public void onBindViewHolder(@NonNull UserAdapter.ViewHolder viewHolder, final int i) {
        final User user = musers.get(i);
        viewHolder.userId.setText(user.getId() + "");
        viewHolder.userName.setText(user.getUserName());
        viewHolder.userPwd.setText(user.getUserPwd());
        viewHolder.userName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent mIntent1 = new Intent("delete");
                mIntent1.putExtra("id", user.getId()+ "");
                mcontext.sendBroadcast(mIntent1);
            }
        });



    }

    //item的总长度
    @Override
    public int getItemCount() {
        return musers.size();
    }

}
